<?php

function mail_to_admin( $config, $subject, $message ) {
	$mail = new Mail();
	$mail->protocol = $config->get('config_mail_protocol');
	$mail->parameter = $config->get('config_mail_parameter');
	$mail->hostname = $config->get('config_smtp_host');
	$mail->username = $config->get('config_smtp_username');
	$mail->password = $config->get('config_smtp_password');
	$mail->port = $config->get('config_smtp_port');
	$mail->timeout = $config->get('config_smtp_timeout');
	$mail->setTo($config->get('config_email'));
	$mail->setFrom($config->get('config_email'));
	$mail->setSender($config->get('config_name'));
	$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
	$mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
	$mail->send();
}

class LogBatch {

  private $_log;

  private function __construct() {
  }

  public function __destruct() {
    if ( $this->_log )
      fclose ( $this->_log );
  }

  public static function &getInstance() {
    static $singleton = null;

    if (!is_object($singleton)) {
      $singleton = new LogBatch();
      $singleton->_log = null;

      if ( FILE_LOG != '' ) {
        $singleton->_log = fopen(FILE_LOG, 'a');
	      if (!$singleton->_log)
	        $singleton->_log = null;
      }
    }

    return $singleton;
  }

  public function write( $msg ) {
	  if ( $this->_log )
      fwrite($this->_log, "$msg\n");
  }

}

function error_handler($errno, $errstr, $errfile, $errline) {
	
	switch ($errno) {
		case E_NOTICE:
		case E_USER_NOTICE:
			$error = 'Notice';
			break;
		case E_WARNING:
		case E_USER_WARNING:
			$error = 'Warning';
			break;
		case E_ERROR:
		case E_USER_ERROR:
			$error = 'Fatal Error';
			break;
		default:
			$error = 'Unknown';
			break;
	}
		
	LogBatch::getInstance()->write('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);

	return true;
}
	
// Error Handler
set_error_handler('error_handler');

// Settings
$query = $db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE store_id = '0' OR store_id = '" . (int)$config->get('config_store_id') . "' ORDER BY store_id ASC");

foreach ($query->rows as $setting) {
  if (!$setting['serialized']) {
    $config->set($setting['key'], $setting['value']);
  } else {
    $config->set($setting['key'], unserialize($setting['value']));
  }
}

$lang = $config->get('config_language');
$language_id = 1;

$query = $db->query("SELECT language_id FROM language where code = '$lang' "); 
if ( $query->num_rows ) {
  $language_id = $query->row['language_id'];
}
